package org.bud.data.bit.fmt;

import org.bud.data.bit.ask.FmtAsk;
import org.bud.data.bit.check.CheckData;

import java.util.Optional;
import java.util.function.UnaryOperator;

/**
 * @author shaun
 * @date 2024/7/21
 * @instruction
 */
public abstract class BaseFmtData<T> implements FmtData<T>{

    protected abstract CheckData def();

    protected abstract UnaryOperator<T> fmt();

    @Override
    public T fmt(Object data, String key, FmtAsk ask) throws Exception {
        if(data == null)
            return null;
        T origin = origin(data);
        CheckData chek = Optional.ofNullable(ask.getCheck()).orElse(def());
        UnaryOperator<T> ft = Optional.ofNullable((UnaryOperator<T>)ask.getFmt())
                .orElse(fmt());
        if(chek == null ||
                !chek.check(data,key) ||
                ft == null){
            return origin;
        }
        return ft.apply(origin);
    }
}
